package com.jcl.cloud.services.crawler.brain.service;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ManageTaskService {

    private final static Logger logger = LoggerFactory.getLogger(ManageTaskService.class);

    @Autowired
    private RedisService redisService;

    public void manageCookieFail(String crawlerId){


        //清除和crawlerId相关的信息
        this.redisService.hdel("unicrawler:task:info:"+ crawlerId,"search-config","catch-new-config","cookieStore");

        this.redisService.del("crawler:brain:search:tasks:"+crawlerId);

        this.redisService.del("crawler:brain:catch-new:tasks:"+crawlerId);


        //让任务休息一个小时后再进行登录爬取
        Long currentTime = System.currentTimeMillis();

        Long  ONE_HOUR_TIMEMILLIS = (long)(3 * 60 * 60 *1000);
        Long nextExecuteTime = currentTime + ONE_HOUR_TIMEMILLIS;
        this.redisService.hset("unicrawler:task:info:"+ crawlerId,"nextExecuteTime",String.valueOf(nextExecuteTime));
        this.redisService.hset("unicrawler:task:info:"+ crawlerId,"taskType","LOGIN");

        //将任务放入队列
        this.redisService.lpush("unicrawler:tasks:all",crawlerId);

        logger.info(String.format("任务号：%s ，cookie失效后处理完成！   下次执行时间为：%s",crawlerId,String.valueOf(nextExecuteTime)));
    }
}
